package com.jason.library.common;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.jason.library.common.ui.JasonToolsActivity;
import com.jason.library.common.utils.LogUtils;
import com.jason.library.crash.test.CrashTestActivity;
import com.jason.library.crash.ui.CrashActivity;
import com.jason.library.crash.ui.CrashDetailsActivity;
import com.jason.library.crash.ui.CrashInfoListActivity;
import com.jason.library.network.ui.NetRequestActivity;
import com.jason.library.network.ui.NetworkDetailActivity;
import com.jason.library.network.ui.NetworkManager;
import com.jason.library.network.utils.NetworkTool;
import com.jason.library.widget.floating.FloatWindow;
import com.jason.library.widget.floating.PermissionListener;
import com.jason.library.widget.floating.Screen;
import com.jason.library.widget.floating.ViewStateListener;
import com.jason.library.widget.floating.ViewStateListenerAdapter;

/**
 * Description:JasonTools
 *
 * @author chenbaoyang
 * @create 2020/12/15 10:10
 */
public class JasonTools {

    private static JasonTools INSTANCE;
    private Application app;

    /**
     * 获取NetworkTool实例 ,单例模式
     */
    public static JasonTools getInstance() {
        if (INSTANCE == null) {
            synchronized (NetworkTool.class) {
                if (INSTANCE == null) {
                    INSTANCE = new JasonTools();
                }
            }
        }
        return INSTANCE;
    }

    public void init(Application application){
        this.app = application;

        NetworkTool.getInstance().init(application);
    }


    public Application getApplication() {
        return app;
    }

    /**
     * 设置全局悬浮按钮，点击可以去网络列表页面
     * @param context                           上下文
     */
    public void setFloat(final Context context){
        ImageView imageView = new ImageView(context.getApplicationContext());
        imageView.setImageResource(R.drawable.ic_show_error);
        FloatWindow
                .with(context)
                .setView(imageView)
                //设置悬浮控件宽高
                .setWidth(Screen.width, 0.2f)
                .setHeight(Screen.width, 0.2f)
                //设置控件初始位置
                .setX(Screen.width, 0.8f)
                .setY(Screen.height, 0.3f)
                //桌面显示
                .setDesktopShow(false)
                //设置这两个页面隐藏
                .setFilter(false, JasonToolsActivity.class,
                        CrashActivity.class,
                        CrashInfoListActivity.class,
                        CrashDetailsActivity.class,
                        CrashTestActivity.class,
                        NetRequestActivity.class ,
                        NetworkDetailActivity.class)
                //监听悬浮控件状态改变
                .setViewStateListener(new ViewStateListenerAdapter())
                .setPermissionListener(new PermissionListener() {
                    @Override
                    public void onSuccess() {
                        LogUtils.i("NetworkTool-------PermissionListener------onSuccess");
                    }

                    @Override
                    public void onFail() {
                        LogUtils.i("NetworkTool-------PermissionListener------onFail");
                    }
                })  //监听权限申请结果
                .build();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(context.getApplicationContext());
            }
        });
    }


    private void start(Context context) {
        try {
            Intent intent = new Intent(context, JasonToolsActivity.class);
            //为activity开启新的栈，Intent.FLAG_ACTIVITY_NEW_TASK 设置状态，
            //首先查找是否存在和被启动的Activity具有相同的任务栈，如果有则直接把这个栈整体移到前台，并保持栈中的状态不变，
            //既栈中的activity顺序不变，如果没有，则新建一个栈来存放被启动的Activity。
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
